/*
 * @Author: Cao Shixin
 * @Date: 2020-12-18 17:23:20
 * @LastEditors: Cao Shixin
 * @LastEditTime: 2021-05-11 13:59:09
 * @Description: 
 */
import 'package:logger_csx/logger_csx.dart';
import 'log_base.dart';
import 'log_model.dart';

/*
 * 类方法调用
 */
class LogUtil {
  /* 抽象处理文件打印存储
   * 
   * message: 打印的信息
   * fileSubName: 自定义分文件保存文件的文件名前缀
   * level： 信息的等级
   */
  static void log(String message,
      {String? fileSubName, String? tag, Level level = Level.debug}) {
    var extraParam =
        LogExtraParam.fromJson({'fileSubName': fileSubName, 'tag': tag})
            .toJson();
    switch (level) {
      case Level.verbose:
        logger.v(message, extraParam: extraParam);
        break;
      case Level.debug:
        logger.d(message, extraParam: extraParam);
        break;
      case Level.info:
        logger.i(message, extraParam: extraParam);
        break;
      case Level.warning:
        logger.w(message, extraParam: extraParam);
        break;
      case Level.error:
        logger.e(message, extraParam: extraParam);
        break;
      case Level.wtf:
        logger.wtf(message, extraParam: extraParam);
        break;
      default:
        throw ArgumentError('Log level error！');
    }
  }

  static void logD(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.debug);
  }

  static void logE(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.error);
  }

  static void logI(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message, fileSubName: fileSubName, tag: tag, level: Level.info);
  }

  static void logV(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.verbose);
  }

  static void logW(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.warning);
  }

  static void logWTF(String message, {String? fileSubName, String? tag}) {
    LogUtil.log(message, fileSubName: fileSubName, tag: tag, level: Level.wtf);
  }
}

/*
 * 对象模式使用，方便统一设置fileSubName和tag
 * 使用方法：LogObject实例的对象，
 */
class LogObject {
  factory LogObject() => _getInstance();
  static LogObject get instance => _getInstance();
  static LogObject? _instance;
  static LogObject _getInstance() {
    if (_instance == null) {
      _instance = LogObject._internal();
    }
    return _instance!;
  }

  String? fileSubName;

  LogObject._internal() {
    // 初始化
    fileSubName = '';
  }

  void log(String message, {String? tag, Level level = Level.debug}) {
    LogUtil.log(message, fileSubName: fileSubName, tag: tag, level: level);
  }

  void logD(String message, {String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.debug);
  }

  void logE(String message, {String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.error);
  }

  void logI(String message, {String? tag}) {
    LogUtil.log(message, fileSubName: fileSubName, tag: tag, level: Level.info);
  }

  void logV(String message, {String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.verbose);
  }

  void logW(String message, {String? tag}) {
    LogUtil.log(message,
        fileSubName: fileSubName, tag: tag, level: Level.warning);
  }

  void logWTF(String message, {String? tag}) {
    LogUtil.log(message, fileSubName: fileSubName, tag: tag, level: Level.wtf);
  }
}
